/**
 * Author: Ravi Tamada
 * URL: www.androidhive.info
 * twitter: http://twitter.com/ravitamada
 * */
package com.vg.login;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.androidhive.jsonparsing.Activity_ChatIndex;
import com.androidhive.jsonparsing.R;
import com.vg.library.JSONParser;
import com.vg.library.UserFunctions;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class LoginActivity extends Activity {
	Button btnLogin;
	Button btnLinkToRegister;
	EditText inputEmail;
	EditText inputPassword;
	TextView loginErrorMsg;
	boolean check = false;
	
	UserFunctions userFunction = new UserFunctions();
	JSONParser parse = new JSONParser();
	// JSON Response node names
	private static String KEY_SUCCESS = "success";
	private static String KEY_ERROR = "error";
	private static String KEY_ERROR_MSG = "error_msg";
	private static String KEY_UID = "uid";
	private static String KEY_NAME = "name";
	private static String KEY_EMAIL = "email";
	private static String KEY_CREATED_AT = "created_at";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		// Importing all assets like buttons, text fields
		inputEmail = (EditText) findViewById(R.id.loginEmail);
		inputPassword = (EditText) findViewById(R.id.loginPassword);
		btnLogin = (Button) findViewById(R.id.btnLogin);
		btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
		loginErrorMsg = (TextView) findViewById(R.id.login_error);
	//	geturl("lananh@gmail.com");
		// Login button Click Event
		btnLogin.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				String email = inputEmail.getText().toString();
				String password = inputPassword.getText().toString();
				String md5=userFunction.getMd5Hash(password);
				Log.d("Button", "Login");
				 JSONArray json = userFunction.loginUser1(email);
			 Log.i("jsonIndex", ""+json.toString());
			//	JSONObject obj_json=userFunction.loginUser(email, password);
			//	Log.i("jsonIndex", ""+obj_json.toString());
				
				
				
				
				JSONArray array = null;
				JSONArray array_user_id = null;
			//	array = userFunction.loginArrayUser(email, password);
				//Log.i("Array Index", "" + array.toString());

				// userFunction.logoutUser(getApplicationContext());
				// Log.i("aray", "" + array.toString());
				
				
				array = parse.getJsonarray("http://192.168.0.138/json/users.php");
				
				
//				for (int i = 0; i < array_user_id.length(); i++) {
//					try {
//						JSONObject obj = array_user_id.getJSONObject(i);
//
//						String email_array_id = obj.getString("use_login").trim();
//						Log.i("array_UserLogin_1111", "" + email_array_id);
//
//						
//					} catch (JSONException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//
//				}

				for (int i = 0; i < array.length(); i++) {
					try {
						JSONObject obj = array.getJSONObject(i);
						String email_array = obj.getString("use_login").trim();
						Log.i("array_UserLogin", "" + email_array);
						String pass_array = obj.getString("use_password")
								.trim();
						Log.i("array_UserLogin", "" + pass_array);
						if (email.equals(email_array) && md5.equals(pass_array)) {
							check = true;
							Log.i("Check", "true");
						}
						
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				if (check == true) {
					 Intent i = new Intent(getApplicationContext(),Activity_ChatIndex.class);
					 i.putExtra("email",email);
						 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						 startActivity(i);
						
						 // Close Login Screen
						 finish();
				}
				else
				{
					Toast.makeText(getBaseContext(), "false",
							Toast.LENGTH_LONG).show();
				}

				// for(int i=0;i<array.length();i++)
				// {
				// try {
				// JSONObject obj=array.getJSONObject(i);
				// String
				// emai_array=obj.getString("use_login").toString().trim();
				// Log.i("Object Json Array",""+emai_array);
				// // if(email==emai_array.toString());
				// // {
				// // Toast.makeText(getBaseContext(), "false",
				// Toast.LENGTH_LONG).show();
				// // Log.i("Kiem Tra", "True");
				// // }
				// // else {
				// // Toast.makeText(getBaseContext(), "false",
				// Toast.LENGTH_LONG).show();
				// // }
				// if (email==obj) {
				// Toast.makeText(getBaseContext(), "true",
				// Toast.LENGTH_SHORT).show();
				// Log.i("Kiem Tra", "True");
				// } else {
				// Toast.makeText(getBaseContext(), "false",
				// Toast.LENGTH_SHORT).show();
				// }
				//
				// } catch (JSONException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				//
				// }

				// check for login response
				// try {
				// if (json.getString(KEY_SUCCESS) != null) {
				// loginErrorMsg.setText("");
				// String res = json.getString(KEY_SUCCESS);
				// if(Integer.parseInt(res) == 1){
				// // user successfully logged in
				// // Store user details in SQLite Database
				// DatabaseHandler db = new
				// DatabaseHandler(getApplicationContext());
				// JSONObject json_user = json.getJSONObject("user");
				//
				// // Clear all previous data in database
				// userFunction.logoutUser(getApplicationContext());
				// db.addUser(json_user.getString(KEY_NAME),
				// json_user.getString(KEY_EMAIL), json.getString(KEY_UID),
				// json_user.getString(KEY_CREATED_AT));
				//
				// // Launch Dashboard Screen
				// Intent dashboard = new Intent(getApplicationContext(),
				// DashboardActivity.class);
				//
				// // Close all views before launching Dashboard
				// dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				// startActivity(dashboard);
				//
				// // Close Login Screen
				// finish();
				// }else{
				// // Error in login
				// loginErrorMsg.setText("Incorrect username/password");
				// }
				// }
				// } catch (JSONException e) {
				// e.printStackTrace();
				// }
			}
		});

		// Link to Register Screen
		btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

			public void onClick(View view) {
				Intent i = new Intent(getApplicationContext(),
						RegisterActivity.class);
				startActivity(i);
				finish();
			}
		});
	}
	
	public void geturl(String email)
	{
		JSONArray array_user_id=parse.getJsonarray("http://192.168.0.138/json/user_info.php?email=lananh@gmail.com");
		for(int i=0;i<array_user_id.length();i++)
		{
			try {
				JSONObject obj=array_user_id.getJSONObject(i);
				String id=obj.getString("use_id");
				Log.i("user_id", id.toString());
				
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}
}
